package com.fenghuajueli.libbasecoreui.mvp;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.fenghuajueli.libbasecoreui.widget.CustomProgressDialog;

public class BaseFragment extends Fragment {

    private Boolean isFirstLoad = true;//是否第一次加载数据

    CustomProgressDialog loadingDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
    }

    /**
     * 显示系统等待框
     */
    public void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new CustomProgressDialog(getContext());
            loadingDialog.setCancelable(false);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            firstLoadData();
            isFirstLoad = false;
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        ARouter.getInstance().inject(context);
    }

    protected void firstLoadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirstLoad = true;
    }

    /**
     * 显示系统等待框
     */
    public void showLoadingDialog(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new CustomProgressDialog(getContext(), msg);
            loadingDialog.setCancelable(false);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }

    }


    /**
     * 显示系统等待框
     */
    public void showLoadingDialog(String msg, boolean isShowProgress) {
        if (loadingDialog == null) {
            loadingDialog = new CustomProgressDialog(getContext(), msg, isShowProgress);
            loadingDialog.setCancelable(false);
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show();
        }

    }

    /**
     * 关闭等待框
     */
    public void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
            loadingDialog = null;
        }
    }


    public void setProgress(int progress) {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.setProgress(progress);
        }
    }

    public void showToast(String content){
        ToastUtils.showShort(content);
    }
}
